{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.7.9 64-bit",
   "metadata": {
    "interpreter": {
     "hash": "0600588c3b5f4418cbe7b5ebc6825b479f3bc010269d8b60d75058cdd010adfe"
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "*[Python官方算法复杂度网站](https://wiki.python.org/moin/TimeComplexity)*  \n",
    "字典的 in 操作复杂度是O(1)  \n",
    "列表 in 操作则与列表规模呈线性关系"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import timeit\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "type   lst_time     d_time\n",
      "1000      0.025      0.003\n",
      "3000      0.042      0.002\n",
      "5000      0.048      0.002\n",
      "7000      0.053      0.001\n",
      "9000      0.065      0.001\n"
     ]
    }
   ],
   "source": [
    "print(\"type\", \"lst_time\".rjust(10), \"d_time\".rjust(10))\n",
    "for i in range(int(1e3), int(1e4), int(2e3)):\n",
    "    t = timeit.Timer(\"random.randrange(%d) in x\"%i, \"from __main__ import random, x\")\n",
    "    x = list(range(i))\n",
    "    lst_time = t.timeit(number=1000)\n",
    "    x = {j:None for j in range(i)}\n",
    "    d_time = t.timeit(number=1000)\n",
    "    print(\"%d %10.3f %10.3f\" % (i, lst_time, d_time))"
   ]
  }
 ]
}